<template>
  <div>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column width="120" align="center" label="消防设施图像">
        <template slot-scope="scope">
          <embed :src="scope.row.imageUrl" style="height:auto;width:100%;">
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="消防设施名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="位置">
        <template slot-scope="scope">
          <span>{{ scope.row.properties.Alocation }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="消防设施ID">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">{{ $t('table.delete') }}</el-button>
          <el-button type="primary" size="mini" @click="handleDetails(scope.row)">{{ $t('table.details') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.pagesize"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="消防设施名称" prop="name">
          <el-input :disabled="true" v-model="temp.name"/>
        </el-form-item>
        <el-form-item label="位置" prop="properties.Alocation">
          <el-input v-model="temp.properties.Alocation"/>
        </el-form-item>
        <el-form-item label="站点ID" prop="siteid">
          <el-input :disabled="true" v-model="listQuery.siteid"/>
        </el-form-item>
        <el-form-item label="位置图片" prop="imageUrl">
          <el-input :disabled="true" v-model="temp.imageUrl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='update'" type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { assetList, addAsset, updateAsset, deleteAsset, searcAssetList } from '@/api/fire/asset/asset'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'AssetList',
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        pagesize: 10,
        name: '',
        siteid: this.$store.state.asset.siteid
      },
      showReviewer: false,
      temp: {
        assetid: '',
        name: '',
        siteid: '',
        longitude: '',
        arange: '',
        alocation: '',
        properties: {
          Alocation: '',
          Arange: '',
          createtime: ''
        },
        imageUrl: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建',
        delete: '删除',
        details: '详情',
        binding: '绑定'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        name: [{ required: true, message: '请输入公司地址', trigger: 'blur' }],
        address: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        longitude: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        latitude: [{ required: true, message: '请输入网站', trigger: 'blur' }],
        levation: [{ required: true, message: '请输入网站', trigger: 'blur' }],
        province: [{ required: true, message: '请输入法人', trigger: 'blur' }]
      },
      dialogFormRolesVisible: false,
      btn_query: false,
      btn_add: false,
      btn_edit: false,
      btn_delete: false
    }
  },
  created() {
    this.getList()
    this.btn_query = this.checkPermission(['supervisor:list'])
    this.btn_add = this.checkPermission(['supervisor:add'])
    this.btn_edit = this.checkPermission(['supervisor:update'])
    this.btn_delete = this.checkPermission(['supervisor:delete'])
  },
  methods: {
    getList() {
      this.listLoading = true
      assetList(this.listQuery).then(response => {
        console.log(response)
        console.log(this.listQuery)
        this.list = response.data.context.assetlists
        this.total = response.data.context.total
        this.listLoading = false
      })
    },
    AssetSearch(val) {
      this.listLoading = true
      this.listQuery.name = val
      searcAssetList(this.listQuery).then(response => {
        console.log(response)
        console.log(this.listQuery)
        this.list = response.data.context.assetlists
        this.total = response.data.context.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        assetid: '',
        name: '',
        siteid: '',
        longitude: '',
        arange: '',
        alocation: '',
        properties: {
          Alocation: '',
          Arange: '',
          createtime: ''
        },
        imageUrl: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addAsset(this.temp).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleDetails(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'details'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.assetid = this.temp.id
          this.temp.siteid = this.$store.state.asset.siteid
          this.temp.alocation = this.temp.properties.Alocation
          this.temp.arange = this.temp.properties.Arange
          this.temp.createtime = this.temp.properties.createtime
          console.log(this.temp)
          const tempData = Object.assign({}, this.temp)
          updateAsset(tempData).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.assetid = row.id
        row.siteid = this.$store.state.asset.siteid
        console.log(row)
        deleteAsset(row).then(response => {
          if (response.data.status === '0') {
            this.getList()
            this.$notify({
              title: '成功',
              message: response.data.msg,
              type: 'success',
              duration: 2000
            })
          } else {
            this.$notify.error({
              title: '失败',
              message: response.data.msg,
              duration: 2000
            })
          }
        })
      })
    }
  }
}
</script>
